<?php
/**
 * Created by PhpStorm.
 * User: fengyan
 * Date: 2017/4/13
 * Time: 下午11:08
 */

namespace app\admin\model;


class AuthGroupAccess extends BaseModel
{
    protected $pk = ['group_id', 'admin_id'];

    /**
     * 根据用户编号获取用户所在组
     * @param $adminId
     * @return array
     */
    public function getGroupIdsByAdminId($adminId)
    {
        if (AdminMember::isAdministrator($adminId)) {
            return [];
        }

        $result = $this->where('admin_id', $adminId)->field('group_id')->select();

        $groupList = [];
        foreach ($result as $key=>$value) {
            $groupList[$key] = $value->group_id;
        }

        return $groupList;
    }

    /**
     * 根据组编号获取用户信息
     * @param $groupId
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getGroupAccessUser($groupId)
    {
        $prefix = config('database.prefix');
        return $this->alias('a')->join($prefix.'auth_group g', 'a.group_id=g.id')->join($prefix.'admin_member m', 'm.id=a.admin_id')->where('a.group_id', $groupId)->where('m.status', 99)->field('a.group_id, a.admin_id, m.nickname, m.email, g.title as group_name')->select();
    }
}